package me.ivps.android.weibo.ui.main;

import me.ivps.android.weibo.support.lib.AppFragmentPagerAdapter;
import me.ivps.android.weibo.ui.maintimeline.CommentsByMeTimeLineFragment;
import me.ivps.android.weibo.ui.maintimeline.CommentsToMeTimeLineFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;

/**
 * User: qii Date: 13-3-8
 */
public class CommentsTimeLinePagerAdapter extends AppFragmentPagerAdapter {
    
    private SparseArray<Fragment> fragmentList;
    
    public CommentsTimeLinePagerAdapter(CommentsTimeLine fragment,
            ViewPager viewPager, FragmentManager fm,
            SparseArray<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
        // 暂时屏蔽错误
        // fragmentList.append(CommentsTimeLine.COMMENTS_TO_ME_CHILD_POSITION,
        // fragment.getCommentsToMeTimeLineFragment());
        fragmentList.append(CommentsTimeLine.COMMENTS_BY_ME_CHILD_POSITION,
                fragment.getCommentsByMeTimeLineFragment());
        FragmentTransaction transaction = fragment.getChildFragmentManager()
                .beginTransaction();
        // 屏蔽错误
        // if (!fragmentList.get(CommentsTimeLine.COMMENTS_TO_ME_CHILD_POSITION)
        // .isAdded()) {
        // transaction.add(viewPager.getId(), fragmentList
        // .get(CommentsTimeLine.COMMENTS_TO_ME_CHILD_POSITION),
        // CommentsToMeTimeLineFragment.class.getName());
        // }
        if (!fragmentList.get(CommentsTimeLine.COMMENTS_BY_ME_CHILD_POSITION)
                .isAdded()) {
            transaction.add(viewPager.getId(), fragmentList
                    .get(CommentsTimeLine.COMMENTS_BY_ME_CHILD_POSITION),
                    CommentsByMeTimeLineFragment.class.getName());
        }
        if (!transaction.isEmpty()) {
            transaction.commit();
            fragment.getChildFragmentManager().executePendingTransactions();
        }
    }
    
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }
    
    @Override
    protected String getTag(int position) {
        SparseArray<String> tagList = new SparseArray<String>();
        tagList.append(CommentsTimeLine.COMMENTS_TO_ME_CHILD_POSITION,
                CommentsToMeTimeLineFragment.class.getName());
        tagList.append(CommentsTimeLine.COMMENTS_BY_ME_CHILD_POSITION,
                CommentsByMeTimeLineFragment.class.getName());
        
        return tagList.get(position);
    }
    
    @Override
    public int getCount() {
        return 2;
    }
    
}
